package middleware

import (
	"iptv_server/v3/v2_src/utils"
	"net/http"
)

type MiddleWare func(h http.Handler) http.Handler

type withLogHanlder struct {
	h http.Handler
}

func (l *withLogHanlder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	utils.Logger.Infof("%s method=%s,path=%s", r.RemoteAddr, r.Method, r.URL.Path)
	l.h.ServeHTTP(w, r)
}

func WithLog(h http.Handler) http.Handler {

	return &withLogHanlder{
		h: h,
	}
}

func ApplyMiddles(h http.Handler, middles ...MiddleWare) http.Handler {
	for _, middle := range middles {
		h = middle(h)
	}
	return h
}
